import { ref } from 'vue'
import { UpdateOrderTypeForm } from './types'
import { moduleTypeSave } from '@/api/basic'

export default () => {
  const updateOrderTypeForm = ref<UpdateOrderTypeForm>({
    id: '',
    name: '',
    parentId: '',
    status: false,
    sort: 0,
  })

  const updateOrderTypeLoading = ref(false)

  const resetUpdateOrderTypeForm = () => {
    updateOrderTypeForm.value = {
      id: '',
      name: '',
      parentId: '',
      status: false,
      sort: 0,
    }
  }

  const updateOrderType = async (orderModuleId: string) => {
    updateOrderTypeLoading.value = true
    const res = await moduleTypeSave(updateOrderTypeForm.value, orderModuleId)
    updateOrderTypeLoading.value = false
    return res.data as { code: number; message: string; success: boolean; data: object }
  }

  return {
    updateOrderTypeForm,
    updateOrderTypeLoading,
    updateOrderType,
    resetUpdateOrderTypeForm,
  }
}
